@tool
class_name SkillComposite
extends SkillAction

#region 属性
## 子动作数组
@export var sub_actions:Array[SkillAction]
## 当前正在执行的分支动作
var cur_branch_action:SkillAction = null
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for i in range(sub_actions.size()):
			sub_actions[i].init(i)
			if not connect_obj_dict.has(sub_actions[i].skill_action_release_completed):
				connect_obj_dict.set(sub_actions[i].skill_action_release_completed,ConnectObj.new(sub_actions[i].skill_action_release_completed,[]))
			var connect_obj:ConnectObj = connect_obj_dict.get(sub_actions[i].skill_action_release_completed) as ConnectObj
			connect_obj.connect_single(on_sub_action_release_completed)
			if not connect_obj_dict.has(sub_actions[i].skill_action_interrupted):
				connect_obj_dict.set(sub_actions[i].skill_action_interrupted,ConnectObj.new(sub_actions[i].skill_action_interrupted,[]))
			connect_obj = connect_obj_dict.get(sub_actions[i].skill_action_interrupted) as ConnectObj
			connect_obj.connect_single(on_sub_action_interrupted)
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		cur_branch_action = get_branch_action()
		if cur_branch_action:
			cur_branch_action.release(args)
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if cur_branch_action:
			cur_branch_action.interrupt()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.complete(release_result,handle_func)
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.cool_complete()
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.reset_cool()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		for action in sub_actions:
			action.destroy()
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	var branch_action = get_branch_action()
	if branch_action:
		var status = branch_action.check_can_release()
		if status != GameEnum.SkillReleaseStatus.Success:
			return GameEnum.SkillReleaseStatus.SubActionCantRelease
		else:
			return status
	return GameEnum.SkillReleaseStatus.Success
func check_is_completed() -> bool:
	return is_completed
#region 公共方法
func check_condition(blackboard:Blackboard) -> int:
	return -1
func get_branch_action() -> SkillAction:
	var result := check_condition(cur_blackboard)
	var action = null if result == -1 or result >= sub_actions.size() else sub_actions[result]
	return action
func on_sub_action_release_completed(skill_action:SkillAction,release_result:GameEnum.SkillReleaseResult):
	if check_is_completed():
		complete(release_result)
func on_sub_action_interrupted(skill_action:SkillAction):
	if check_is_completed():
		complete(GameEnum.SkillReleaseResult.Interrupted)
func on_sub_actions_changed(node:Node):
	var subs = []
	subs = get_children().filter(func(x):return x is SkillAction)
	sub_actions.clear()
	sub_actions.append_array(subs)
func on_sub_actions_order_changed():
	var subs = []
	subs = get_children().filter(func(x):return x is SkillAction)
	sub_actions.clear()
	sub_actions.append_array(subs)
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	if not connect_obj_dict.has(child_entered_tree):
		connect_obj_dict.set(child_entered_tree,ConnectObj.new(child_entered_tree,[]))
		var connect_obj:ConnectObj = connect_obj_dict.get(child_entered_tree) as ConnectObj
		connect_obj.connect_single(on_sub_actions_changed)
	if not connect_obj_dict.has(child_exiting_tree):
		connect_obj_dict.set(child_exiting_tree,ConnectObj.new(child_exiting_tree,[]))
		var connect_obj:ConnectObj = connect_obj_dict.get(child_exiting_tree) as ConnectObj
		connect_obj.connect_single(on_sub_actions_changed)
	if not connect_obj_dict.has(child_order_changed):
		connect_obj_dict.set(child_order_changed,ConnectObj.new(child_order_changed,[]))
		var connect_obj:ConnectObj = connect_obj_dict.get(child_order_changed) as ConnectObj
		connect_obj.connect_single(on_sub_actions_order_changed)
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	if sub_actions.size() < 1:
		warnings.append("此节点至少需要一个子节点")
	return warnings
#endregion
